package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.dubbo.api.PeachbossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.Peachbossom;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PeachbossomVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class PeachblossomService {

    @DubboReference
    private PeachbossomApi peachbossomApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //发送语言
    public void peachblossomfs(MultipartFile soundFile,HttpServletResponse response) throws IOException {
        Long userId = UserHolder.getUserId();
        String url = ossTemplate.upload(soundFile.getOriginalFilename(), soundFile.getInputStream());
        System.out.println(url);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Integer a = null;
        String num = redisTemplate.opsForValue().get("Count-" + format + "-" + UserHolder.getUserId());
        if (num != null && Integer.parseInt(num) >= 0){
            int i = Integer.parseInt(num);
            a = i - 1;
            if (a < 0){
                throw new BusinessException(ErrorResult.noNum());
            }
            redisTemplate.opsForValue().set("Count-"+format+"-"+UserHolder.getUserId(),String.valueOf(a));
        }else {
            redisTemplate.opsForValue().set("Count-"+format+"-"+UserHolder.getUserId(),"3");
            String num2 = redisTemplate.opsForValue().get("Count-" + format+"-" + UserHolder.getUserId());
            int integer = Integer.parseInt(num2);
            a = integer - 1;
            String s = String.valueOf(a);
            redisTemplate.opsForValue().set("Count-"+format+"-"+UserHolder.getUserId(),s);
        }
        peachbossomApi.save(userId,url);
        throw new BusinessException(ErrorResult.Num(a));
    }

    //接受语音
    public PeachbossomVo peachblossomjs() {
        List<Peachbossom> list = peachbossomApi.peachblossomjs();
        if (CollUtil.isEmpty(list)){
            throw new BusinessException(ErrorResult.error());
        }
        Collections.shuffle(list);

        Peachbossom peachbossom = list.get(0);
        Long userid = peachbossom.getUserid();
        if (userid == UserHolder.getUserId()){
            userid = list.get(1).getUserid();
        }
        UserInfo userInfo = userInfoApi.findById(userid);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        String num = redisTemplate.opsForValue().get("Count_" + format + "_" + UserHolder.getUserId());
        PeachbossomVo init = null;
        if (num != null && Integer.parseInt(num) >= 0){
            int i = Integer.parseInt(num);
            init = PeachbossomVo.init(userInfo, peachbossom, i);
            Integer a = i - 1;
            if (a < 0){
                throw new BusinessException(ErrorResult.noNum());
            }
            redisTemplate.opsForValue().set("Count_"+format+"_"+UserHolder.getUserId(),String.valueOf(a));
        }else {
            redisTemplate.opsForValue().set("Count_"+format+"_"+UserHolder.getUserId(),"3");
            String num2 = redisTemplate.opsForValue().get("Count_" + format+"_" + UserHolder.getUserId());
            int integer = Integer.parseInt(num2);
            init = PeachbossomVo.init(userInfo, peachbossom, integer);
            int i = integer - 1;
            String s = String.valueOf(i);
            redisTemplate.opsForValue().set("Count_"+format+"_"+UserHolder.getUserId(),s);
        }
        return init;
    }
}
